import Foundation

protocol MPConvertibleString {}

extension String : MPConvertibleString {}

extension NSString : MPConvertibleString {}

func mp_string(_ value : Any, type : Any.Type) -> MPConvertibleString? {
    var str: String
    switch value {
    case let url as URL: str = url.standardizedFileURL.absoluteString
    case let date as Date: str = "\(Int64(date.timeIntervalSince1970))"
    default: str = "\(value)"
    }
    return type is NSMutableString.Type ? NSMutableString(string: str) : str
}

func mp_dictToStr(_ dic:[String:Any]) ->String? {
    if let data = try? JSONSerialization.data(withJSONObject: dic, options: []) {
        return String.init(data: data, encoding: .utf8)
    }
    return nil
}

func mp_valueToString(_ value : Any) -> String? {
    var str: String? = nil
    switch value {
        case let url as URL: str = url.standardizedFileURL.absoluteString
        case let date as Date: str = "\(Int64(date.timeIntervalSince1970))"
        case _ as MPConvertibleArray: str = nil
        case _ as MPConvertibleDictionary: str = nil
        default: str = "\(value)"
    }
    return str
}
